import router from './router'
import store from './store'
import { initTheme } from './utils/theme'
import 'nprogress/nprogress.css' // Progress 进度条样式
import { getToken, getNav } from '@/utils/auth'
const loginForm = {
  username: '',
  password: ''
}

router.beforeEach((to, from, next) => {
  if (!store.getters.userid) { // 判断当前用户是否已拉取完user_info信息
    if (getToken()) {
      store.dispatch('GetUserInfo').then(res => { // 拉取user_info
        /*
          h5ds获取token接口
          axios.post('http://localhost:7000/api/v1/third/partner-login', {
            'userId': store.getters.userid
          }, {
            headers: {
              Authorization: 'Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI6',
              'Content-Type': 'application/json'
            }
          }).then(function(res) {
            console.log(res)
            sessionStorage.setItem('h5dsToken', res.data.token)
          })
        */
        store
          .dispatch('GetUserSubsys')
          .then(res => {
            let subSysName = ''
            if (getNav('navVal')) {
              subSysName = getNav('navVal')
            } else if (getNav('dropNum')) {
              subSysName = getNav('dropVal')
            } else {
              subSysName = res.data[0].subsys_id
            }
            if (getNav('navNum')) {
              store.dispatch('ChangeActiveMenuIndex', parseInt(getNav('navNum'))).then(() => {})
            }
            if (getNav('dropNum')) {
              store.dispatch('ChangeActiveMenuIndex', 'null').then(() => {})
            }
            store.dispatch('GenerateRoutes', subSysName).then(() => { // 生成可访问的路由表
              router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
              store.dispatch('ChangeMenuLoading', false)
              // const routerlist = store.getters.addRouters
              // next({ path: routerlist[0].path, replace: true }) // hack方法 确保addRoutes已完成 ,replace: true so the navigation will not leave a history record
              next({
                ...to,
                replace: true
              }) // hack方法 确保addRoutes已完成 ,replace: true so the navigation will not leave a history record
              // setTimeout(() => {
              //   initTheme()
              // }, 200)
            })
          })
      }).catch(() => {
      })
    } else {
      store.dispatch('CASLoginByUsername', loginForm).then(() => {
        store.dispatch('GetUserInfo').then(res => { // 拉取user_info
          store
            .dispatch('GetUserSubsys')
            .then(res => {
              let subSysName = ''
              if (getNav('navVal')) {
                subSysName = getNav('navVal')
              } else if (getNav('dropNum')) {
                subSysName = getNav('dropVal')
              } else {
                subSysName = res.data[0].subsys_id
              }
              if (getNav('navNum')) {
                store.dispatch('ChangeActiveMenuIndex', parseInt(getNav('navNum'))).then(() => {})
              }
              if (getNav('dropNum')) {
                store.dispatch('ChangeActiveMenuIndex', 'null').then(() => {})
              }
              store.dispatch('GenerateRoutes', subSysName).then(() => { // 生成可访问的路由表
                router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
                store.dispatch('ChangeMenuLoading', false)
                // const routerlist = store.getters.addRouters
                // next({ path: routerlist[0].path, replace: true }) // hack方法 确保addRoutes已完成 ,replace: true so the navigation will not leave a history record
                next({
                  ...to,
                  replace: true
                }) // hack方法 确保addRoutes已完成 ,replace: true so the navigation will not leave a history record
                // setTimeout(() => {
                //   initTheme()
                // }, 200)
              })
            })
        }).catch(() => {
        })
      })
    }
  } else {
    next()
  }
})
